package dev.caobaoqi6040.backend.modules.user.service.impl;

import dev.caobaoqi6040.backend.modules.user.entity.User;
import dev.caobaoqi6040.backend.modules.user.exception.AuthException;
import dev.caobaoqi6040.backend.modules.user.service.AuthService;
import dev.caobaoqi6040.backend.modules.user.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * AuthServiceImpl
 *
 * @author caobaoqi6040
 * @since 2025/10/9 14:52
 */
@Service
@RequiredArgsConstructor
public class AuthServiceImpl implements AuthService {

	private final UserService service;
	private final PasswordEncoder encoder;

	@Override
	public User signIn(String email, String password) {
		try {
			User user = service.getUserByEmail(email);
			if (!encoder.matches(password, user.getPassword())) {
				throw new AuthException("登陆失败,用户名或密码错误");
			}
			return user;
		} catch (UsernameNotFoundException ex) {
			throw new AuthException("登陆失败,用户名或密码错误");
		}
	}

	@Override
	public void signup(String email, String password) {
		service.makeUser(email, password);
	}

	@Override
	public void verilyCode(String code) {

	}
}
